<?php
namespace app\crm\controller;
use app\common\controller\UserBase; 
use app\crm\model\CrmCustomer;
use app\crm\model\CrmContract;
use app\crm\model\CrmUser;
use app\user\model\FieldData;
use app\user\model\User;

class Contract extends UserBase
{
    public function initialize()
    {
        parent::initialize();
        //获取客服列表
        $this->contact = get_dict_list('6');
    }
    
    public function index_json($customer_id='',$limit='')
    {
        $cc = new CrmContract();
        if($customer_id <> ''){
            $cc = $cc->where('customer_id', $customer_id);
        }
        $list = $cc->with(['user'])->order('id desc')->paginate($limit);
        $this->result($list);
    }
    //添加客户
    public function add($customer_id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['user_id'] = UserId();
            $param['shop_id'] = ShopId();
            $param['customer_id'] = $customer_id;
            $result = CrmContract::create($param);
            if ($result == true) {
                insert_notice($param['end_time'], '1', ['customer_id'=>$customer_id,'id'=>$result->id,'show'=>'contract'], '客户合同到期');
                insert_user_log('添加了合同信息');
                $this->success('添加成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', [
        ]);
    }
    //编辑联系人
    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = CrmContract::update($param,['id'=>$param['id']]);
            if ($result == true) {
                insert_user_log('修改了合同');
                $this->success('修改成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        $data = CrmContract::where('id', $id)->find();
        return $this->fetch('save', [
            'data'      => $data
        ]);
    }
    //删除客户
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CrmContract::destroy($param['id']);
            insert_user_log('删除了合同');
            $this->success('删除成功');
        }
    }
    // 审核合同
    public function verify()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CrmContract::where(['id'=>$param['id']])->update(['status'=>'1']);
            insert_user_log('审核了合同');
            $this->success('审核成功');
        }
    }
    
    
}
